package org.csu.storemanage.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.csu.storemanage.pojo.CartItem;
import org.csu.storemanage.pojo.Item;
import org.csu.storemanage.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSON;
import org.springframework.web.multipart.MultipartFile;


import java.io.IOException;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;


@Controller
public class ItemController {

    @Autowired
    private CartService.ItemService itemService;

    @RequestMapping(value = "/item")
    public String item(HttpServletRequest request, @ModelAttribute("productId")int productId){
        HttpSession session = request.getSession();
        String username = (String)session.getAttribute("username");
        if(username == null)
        {
            session.setAttribute("errMsg","用户未登录");
            return "login";
        }
//        String product_Id = productId;
//        if(product_Id == null)product_Id = (String)session.getAttribute("productId");
        Item item = itemService.getItem(productId);
        session.setAttribute("item",item);
        session.setAttribute("productId",productId);

        //获取评论
        if(username != null) session.setAttribute("username",username);

        return "itemPage";
    }


    @RequestMapping(value = "/itemManage")
    public String itemManage(HttpServletRequest request){
        return "ItemManager";
    }

    @RequestMapping(value = "/itemGet")
    @ResponseBody
    public void ItemGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String categoryId = request.getParameter("categoryId");
        System.out.println("categoryId " + categoryId);
        List<Item> itemList = itemService.getItemListByCategoryId(categoryId);
        String json = JSON.toJSONString(itemList);
        response.setContentType("text/Json");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().print(json);
    }


    @RequestMapping(value = "/addImage")
    @ResponseBody
    public void addImage(@RequestParam("file")MultipartFile file){
        //获取文件名和保存路径
        String fileName = file.getOriginalFilename();
        String uploadDir = "src/main/resources/static/image/";
        String uploadDir2 = "target/classes/static/image/";
        //将文件保存到指定目录下
        try {
            byte[] bytes = file.getBytes();
            Path path1 = Paths.get(uploadDir + fileName);
            Path path2 = Paths.get(uploadDir2 + fileName);
            Files.write(path1,bytes);
            Files.write(path2,bytes);
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "/editItem")
    @ResponseBody
    public void editItem(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String oldName = request.getParameter("oldName");
        String state = request.getParameter("state");
        String limit = request.getParameter("limit");
        int quantity = Integer.parseInt(request.getParameter("quantity"));
        String description = request.getParameter("description");
        int unitPrice = Integer.parseInt(request.getParameter("unitPrice"));
        String newName = request.getParameter("newName");
        System.out.println(newName + oldName);
        itemService.editItem(oldName,quantity,description,state,limit,newName, BigDecimal.valueOf(unitPrice));
    }

    @RequestMapping(value = "/addItem")
    @ResponseBody
    public void addItem(HttpServletRequest request){
        String name = request.getParameter("name");
        String categoryId = request.getParameter("categoryId");
        int quantity = Integer.parseInt(request.getParameter("quantity"));
        BigDecimal unitPrice = BigDecimal.valueOf(Integer.parseInt(request.getParameter("unitPrice")));
        String description = request.getParameter("description");
        String states = request.getParameter("states");
        String limits = request.getParameter("limits");
        itemService.addItem(categoryId,name,quantity,unitPrice,description,states,limits);
    }

    @RequestMapping(value="/deleteCategory")
    @ResponseBody
    public void deleteCategory(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String categoryId = request.getParameter("categoryId");
        itemService.deleteCategory(categoryId);
        response.getWriter().print("successDeleteCategory");
    }

    @RequestMapping(value = "/deleteItemByName")
    @ResponseBody
    public void deleteItemByName(HttpServletRequest request,HttpServletResponse response) throws IOException {
        System.out.println(111);
        String itemName = request.getParameter("itemName");
        System.out.println(itemName);
        itemService.deleteItemByName(itemName);
        response.getWriter().print("successDeleteItem");
    }
}
